<template>
  <!--招采管理-物资招采列表-发标弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="8">
            <a-form-model-item label="招标名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入招标名称"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="8">
            <a-form-model-item label="结标时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="traneIncTime">
              <j-date placeholder="请选择结标时间" v-model="model.traneIncTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="project">
              <a-input v-model="model.project" placeholder="请输入项目"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24" style="margin-top: 20px">
            <div class="table-page-search-wrapper">
              <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                  <a-col :md="6" :sm="12">
                    <a-form-item label="名称">
                      <j-input placeholder="输入名称模糊查询" v-model="queryParam.name"></j-input>
                    </a-form-item>
                  </a-col>
                  <a-col :md="6" :sm="8">
                    <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                      <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px"
                        >重置</a-button
                      >
                    </span>
                  </a-col>
                </a-row>
              </a-form>
            </div>
            <a-table
              ref="table"
              size="middle"
              :scroll="{ x: true }"
              bordered
              rowKey="id"
              :columns="columns"
              :dataSource="dataSource"
              :pagination="ipagination"
              :loading="loading"
              :row-key="(record) => record.id"
              :row-selection="{
                selectedRowKeys: selectedRowKeys,
                onChange: onSelectChange,
                onSelect: onSelect,
                onSelectAll: onSelectAll
              }"
              class="j-table-force-nowrap"
              @change="handleTableChange"
              v-model="model.supplier"
            >
            </a-table>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
export default {
  name: 'WtPublicBiddingForm',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDate
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    id: {
      type: String
    }
  },
  data() {
    return {
      dictOptions: {},
      superFieldList: [],
      queryParam: { type: '0', zhaobiaoId: this.id },
      selectedRowKeys: [],
      selectedRows: [],
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        // {
        //   title: 'id',
        //   align: 'center',
        //   dataIndex: 'id'
        // },
        {
          title: '名称',
          align: 'center',
          dataIndex: 'name'
        },

        {
          title: '联系人',
          align: 'center',
          dataIndex: 'relation'
        },
        {
          title: '电话',
          align: 'center',
          dataIndex: 'phone'
        },
        {
          title: '地址',
          align: 'center',
          dataIndex: 'address'
        },
        {
          title: '评分',
          align: 'center',
          dataIndex: 'score'
        }
      ],

      model: {
        supplier: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      supplierData: [],
      // supplierList:[],
      url: {
        list: '/wtSupplier/list',
        add: '/bidding/wtMaterialsSupplierPrice/add',
        edit: '/bidding/wtMaterialsSupplierPrice/add',
        queryById: '/bidding/wtPublicBidding/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },

  beforeMount(){

  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    // console.log('111', this.id)
  },
  methods: {
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'name', text: '名称', dictCode: '' })
      fieldList.push({ type: 'int', value: 'type', text: '0经营产品1施工', dictCode: '' })
      fieldList.push({ type: 'string', value: 'relation', text: '联系人', dictCode: '' })
      fieldList.push({ type: 'string', value: 'phone', text: '电话', dictCode: '' })
      fieldList.push({ type: 'string', value: 'address', text: '地址', dictCode: '' })
      fieldList.push({ type: 'string', value: 'score', text: '评分', dictCode: '' })
      this.superFieldList = fieldList
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    // 勾选触发的函数
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    /**
     * 表格单选事件
     * @param record 行数据
     * @param selected 是否选中
     */
    onSelect(record, selected) {
      // 若选中，直接push
      if (selected) {
        this.selectedRows.push(record)
      } else {
        // 若取消选中，则查找到这条数据删除
        let index = this.selectedRows.findIndex((item) => item.id == record.id)
        if (index >= 0) {
          this.selectedRows.splice(index, 1)
        }
      }
    },
    /**
     * 表格多选事件
     * @param selected     是否选中
     * @param selectedRows 忘记了，当时没用到
     * @param changeRows   变化行数据
     */
    onSelectAll(selected, selectedRows, changeRows) {
      // 若选中，直接push
      if (selected) {
        // 单选中部分几条数据，此时点击全选，将 changeRows 剩下的几条为选中数据选中
        this.selectedRows = this.selectedRows.concat(changeRows)
      } else {
        // 若取消选中，则为空
        this.selectedRows = []
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'post'
          }
          that.model.supplier = that.selectedRowKeys.join(',')
          httpAction(httpurl, that.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
